package com.aracky.common.util;

import com.aracky.common.config.HttpsClientRequestFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * @author zengzhiping
 * @ClassName: HttpUtil
 * @Description: http访问工具类
 * @date 2021/11/8 16:43
 */
@Slf4j
public class HttpUtil {

    //https
    private static final RestTemplate restHttpsTemplate = new RestTemplate(new HttpsClientRequestFactory());

    //http
    private static final RestTemplate restHttpTemplate = new RestTemplate();

    /**
     * 使用https协议发送post
     *
     * @param url   请求地址
     * @param param 参数
     */
    public static String sendPostHttps(String url, Map<String, Object> param) {
        //编码为utf-8
        restHttpsTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(param, headers);
        ResponseEntity<String> entity = restHttpsTemplate.postForEntity(url, request, String.class);
        //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串；
        return entity.getBody();
    }

    /**
     * 使用https协议发送get
     *
     * @param url   请求地址
     * @param param 参数
     */
    public static String sendGetHttps(String url, Map<String, Object> param) {
        //编码为utf-8
        restHttpsTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        //处理参数
        StringBuilder builder = new StringBuilder();
        if (param != null) {
            for (String key : param.keySet()) {
                builder.append(key + "=" + param.get(key) + "&");
            }
        }
        String substring = builder.substring(0, builder.lastIndexOf("&"));
        url = url + "?" + substring;
        log.info("获取url: " + url);

        ResponseEntity<String> res = restHttpsTemplate.getForEntity(url, String.class);
        return res.getBody();
    }


    /**
     * 使用http协议发送post--批量保存
     *
     * @param url   请求地址
     * @param param 参数
     */
    public static String sendPostHttp(String url, List param, String token) {
        //编码为utf-8
        restHttpTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set(JWTUtil.header,token);
        HttpEntity<List> request = new HttpEntity<>(param, headers);
        ResponseEntity<String> entity = restHttpTemplate.postForEntity(url, request, String.class);
        //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串；
        return entity.getBody();
    }

}
